// 后台管理路由
const express = require('express');
const router = express.Router();
const { verifyToken: ensureAuth, isAdmin: ensureAdmin } = require('../middlewares/auth');
const dashboardController = require('../controllers/dashboardController');
const userController = require('../controllers/userController');
const animalController = require('../controllers/animalController');
const categoryController = require('../controllers/categoryController');
const imageController = require('../controllers/imageController');
const { Op } = require('sequelize');

// 仪表盘
router.get('/', ensureAuth, ensureAdmin, dashboardController.showDashboard);

// 用户管理
router.get('/users', ensureAuth, ensureAdmin, userController.showUsers);
router.get('/users/add', ensureAuth, ensureAdmin, userController.showAddUser);
router.post('/users/add', ensureAuth, ensureAdmin, userController.addUser);
router.get('/users/edit/:id', ensureAuth, ensureAdmin, userController.showEditUser);
router.post('/users/edit/:id', ensureAuth, ensureAdmin, userController.updateUser);
router.get('/users/delete/:id', ensureAuth, ensureAdmin, userController.deleteUser);

// 动物管理
router.get('/animals', ensureAuth, ensureAdmin, animalController.showAnimals);
router.get('/animals/add', ensureAuth, ensureAdmin, animalController.showAddAnimal);
router.post('/animals/add', ensureAuth, ensureAdmin, animalController.addAnimal);
router.get('/animals/edit/:id', ensureAuth, ensureAdmin, animalController.showEditAnimal);
router.post('/animals/edit/:id', ensureAuth, ensureAdmin, animalController.updateAnimal);
router.get('/animals/delete/:id', ensureAuth, ensureAdmin, animalController.deleteAnimal);

// 图片管理
router.get('/animals/:animalId/images', ensureAuth, ensureAdmin, imageController.showImages);
router.post('/animals/:animalId/images/add', ensureAuth, ensureAdmin, imageController.addImage);
router.post('/animals/:animalId/images/update/:imageId', ensureAuth, ensureAdmin, imageController.updateImage);
router.get('/animals/:animalId/images/delete/:imageId', ensureAuth, ensureAdmin, imageController.deleteImage);

// 分类管理
router.get('/categories', ensureAuth, ensureAdmin, categoryController.showCategories);
router.get('/categories/add', ensureAuth, ensureAdmin, categoryController.showAddCategory);
router.post('/categories/add', ensureAuth, ensureAdmin, categoryController.addCategory);
router.get('/categories/edit/:id', ensureAuth, ensureAdmin, categoryController.showEditCategory);
router.post('/categories/edit/:id', ensureAuth, ensureAdmin, categoryController.updateCategory);
router.get('/categories/delete/:id', ensureAuth, ensureAdmin, categoryController.deleteCategory);

module.exports = router;